package com.zw.pdm.module.est.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.sys.User;
import com.zw.pdm.module.est.model.InputCostsModel;
import com.zw.pdm.module.est.service.InputCostsService;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.report.service.SheetService;
import com.zw.pdm.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 控制层
 * @Author: China.kxx
 * @Date: 2020/11/04 20:33
 */
@Slf4j
@Controller
@RequestMapping("/craft/dataEntry/solderingReality")
public class InputCostsRealityController extends BaseController {


    //页面文件路径
    private String prefix = "/craft/dataEntry";
    @Autowired
    private InputCostsService inputCostsService;
    @Autowired
    private SheetService sheetService;
    //加载页面
    @GetMapping("/page")
    public String solderingPlan(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/solderingReality";
    }

    @GetMapping("/project/list")
    @ResponseBody
    public List<Project> proList(String q) {
        return sheetService.findProjectByCode(q);
    }


    /**
     * 加载工时列表数据
     * @param inputCostsModel
     * @return
     */
    @ResponseBody
    @PostMapping("/manpower")
    public JsonResult manpower(InputCostsModel inputCostsModel) {
        List<InputCostsModel> list = inputCostsService.inputCuttingTime(inputCostsModel);
        return jsonResult(list);
    }
    /**
     * 加载辅材列表数据
     * @param inputCostsModel
     * @return
     */
    @ResponseBody
    @PostMapping("/material")
    public JsonResult material(InputCostsModel inputCostsModel) {
        List<InputCostsModel> list = inputCostsService.inputCuttingMaterial(inputCostsModel);
        return jsonResult(list);
    }
    /**
     * 工时编辑模态框数据回显
     * @param manId
     * @return
     */
    @ResponseBody
    @PostMapping("/oneManpowerTime")
    public InputCostsModel selectOneManpowerTime(Integer id) {
        return inputCostsService.selectOneManpowerTime(id);
    }
    /**
     * 辅材编辑模态框数据回显
     * @param mid
     * @return
     */
    @ResponseBody
    @PostMapping("/oneMaterial")
    public InputCostsModel selectOneMaterial(Integer mid) {
        return inputCostsService.selectOneMaterial(mid);
    }


    /**
     *保存添加人工数据，验证当数据被接收无法编辑
     * @param manpower
     * @return
     */
    @ResponseBody
    @PostMapping("/manpowerSave")
    public JsonResult saveMan(InputCostsModel manpower){

        int flg=inputCostsService.selectVerify(manpower);
        if (flg == 1) {
            return error("数据已被接收，无法进行编辑！！需联系商务科");
        } else {
            return jsonResult(inputCostsService.saveManpowerInfo(manpower));
        }
    }

    /**
     * 工时数据删除(单条)
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/delManHour")
    public JsonResult delManHour(Integer manId) {
        int row = inputCostsService.delManHour(manId);
        if (row == 1) {
            return error("数据已被接收，无法进行删除！！需联系商务科");
        } else {
            return jsonResult(inputCostsService.deleteManHour(manId));
        }
    }

    /**
     * 辅材数据删除(单条)
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/delMaterial")
    public JsonResult delMaterial(Integer mid) {
        int row = inputCostsService.delMaterial(mid);
        if (row ==1) {
            return error("数据已被接收，无法进行删除！！需联系商务科");
        } else {
            return jsonResult(inputCostsService.deleteMaterial(mid)) ;
        }

    }
    /**
     * 工时数据删除(全部清空)
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/delManHourAll")
    public JsonResult delManHourAll(InputCostsModel inputCostsModel) {
        int row = inputCostsService.delManHourAll(inputCostsModel);
        if (row == -1) {
            return error("数据已被接收，无法进行删除！！需联系商务科");
        } else if(row == 0){
            return error("无数据，操作失败");
        } else if(row == 1){
            return jsonResult(inputCostsService.deleteManHourAll(inputCostsModel));
        } else {
            return JsonResult.fail();
        }
    }

    /**
     * 辅材数据删除(全部清空)
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/delMatAll")
    public JsonResult delMatAll(InputCostsModel inputCostsModel) {
        int row = inputCostsService.delMatAll(inputCostsModel);
        if (row == -1) {
            return error("数据已被接收，无法进行删除！！需联系商务科");
        } else if(row == 0){
            return error("无数据，操作失败");
        } else if(row == 1){
            return jsonResult(inputCostsService.deleteMatAll(inputCostsModel)) ;
        } else {
            return JsonResult.fail();
        }

    }
    /**
     * 人工数据确认
     *
     */
    @ResponseBody
    @PostMapping("/solderingVerify")
    public JsonResult solderingVerify(InputCostsModel inputCostsModel) {
        return jsonResult(inputCostsService.solderingVerify(inputCostsModel));
    }

    /** 查询部分 */
    @GetMapping("/part")
    @ResponseBody
    public List<DsnProStruct> partList(String code, String type) {
        return inputCostsService.findPartByProjectCode(code, type);
    }

}